﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
using System.IO;
using System.Drawing;

namespace SportsStore.WebUI.Controllers
{
    [Authorize]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        private IProductRepository repository;

        public AdminController(IProductRepository repo)
        {
            repository = repo;
        }

        public ActionResult Index()
        {
            return View(repository.Products);
        }

        //
        // GET: /Admin/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Admin/Create

        public ActionResult Create()
        {
            return View("Edit", new Product());
        } 

        
        //
        // GET: /Admin/Edit/5
 
        public ViewResult Edit(int productId)
        {
            Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productId);
            return View(prod);
        }

        //
        // POST: /Admin/Edit/5

        [HttpPost]
        public ActionResult Edit(Product prod, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                Product dbData = repository.Products.FirstOrDefault(p => p.ProductID == prod.ProductID);

                if (image != null)
                {
                    prod.ImageMimeType = image.ContentType;
                    prod.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(prod.ImageData, 0, image.ContentLength);
                } 
               
 


                //byte[] imgByte = new byte[image.InputStream.Length];
                
                //using (Image i = Image.FromStream(image.InputStream))
                //{
                //    BinaryReader reader = new BinaryReader(image.InputStream);
                //    image.InputStream.Seek(0, SeekOrigin.Begin);
                //    imgByte = reader.ReadBytes((int)image.InputStream.Length);
                //}

                //prod.ImageData = imgByte;
                //prod.ImageMimeType = image.ContentType;

                if (dbData != null)
                {
                    dbData.Category = prod.Category;
                    dbData.Description = prod.Description;
                    dbData.Name = prod.Name;
                    dbData.Price = prod.Price;
                    if (image != null)
                    {
                        dbData.ImageData = prod.ImageData;
                        dbData.ImageMimeType = prod.ImageMimeType;
                    }
                }

                repository.SaveProduct(prod);
                TempData["message"] = string.Format("{0} has been saved", prod.Name);
                return RedirectToAction("Index");
            }
            else
            {
                return View(prod);
            }
        }

        //
        // POST: /Admin/Delete/5

        [HttpPost]
        public ActionResult Delete(int productId)
        {
            Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productId);
            if (prod != null)
            {
                repository.DeleteProduct(prod);
                TempData["message"] = string.Format("{0} was deleted", prod.Name);
            }
            return RedirectToAction("Index"); 
        }
    }
}
